/**
 * Copyright &copy; 2013-2016 山东易科德软件有限公司 All rights reserved.
 */
package cn.net.ecode.modules.cms.entity;

import java.util.List;

import javax.validation.constraints.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.DataEntity;
import cn.net.ecode.common.persistence.annotation.Id;
import cn.net.ecode.modules.sys.utils.CorpUtils;
import cn.net.ecode.modules.sys.utils.UserUtils;

/**
 * 站点Entity
 * @author ThinkGem
 * @version 2014-9-16
 */
public class Site extends DataEntity<Site> {

	/**
	 * 获取默认站点编码
	 */
	public static final String MAIN_SITE_CODE = Global.getConfig("cms.mainSiteCode", "1");
	
    /**
   	 * 模板路径
   	 */
   	public static final String TPL_BASE = "/WEB-INF/views/modules/cms/front/themes";
   	
    public static final String DEFAULT_TEMPLATE = "frontIndex";
    
	private static final long serialVersionUID = 1L;
	
	private String siteCode;	// 站点名称
	private String siteName;	// 站点名称
	private String viewCode;  // 显示的代码名称（仅作为显示用，集团内唯一）
	private String title;	// 站点标题
	private String logo;	// 站点logo
	private String domain;	// 域名
	private String description;// 描述，填写有助于搜索引擎优化
	private String keywords;// 关键字，填写有助于搜索引擎优化
	private String theme;	// 主题
	private String copyright;// 版权信息
	private String customIndexView;// 自定义首页视图文件
	
	private List<String> categoryCodes;//某角色下某站点下授权的栏目id集合
	private List<Category> categoryList;//某站点下所有栏目集合

	public Site() {
		super();
	}
	
	public Site(String id){
		super(id);
	}

	@Id
	public String getSiteCode() {
		return siteCode;
	}

	public void setSiteCode(String siteCode) {
		this.siteCode = siteCode;
	}

	@NotBlank
	@Length(min=0, max=100)
	public String getSiteName() {
		return siteName;
	}

	public void setSiteName(String siteName) {
		this.siteName = siteName;
	}

	@Pattern(regexp="[a-zA-Z0-9_]{0,30}", message="编码长度不能大于 30 个字符，并且只能包含字母、数字、下划线")
	public String getViewCode() {
		return viewCode;
	}

	public void setViewCode(String viewCode) {
		this.viewCode = viewCode;
	}

	@NotBlank
	@Length(min=0, max=100)
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
	
	public String getLogo() {
		return logo;
	}

	public void setLogo(String logo) {
		this.logo = logo;
	}

	public String getDomain() {
		return domain;
	}

	public void setDomain(String domain) {
		this.domain = domain;
	}

	@Length(min=0, max=255)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Length(min=0, max=255)
	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	@NotBlank
	@Length(min=0, max=255)
	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

	public String getCopyright() {
		return copyright;
	}

	public void setCopyright(String copyright) {
		this.copyright = copyright;
	}

	public String getCustomIndexView() {
		return customIndexView;
	}

	public void setCustomIndexView(String customIndexView) {
		this.customIndexView = customIndexView;
	}
	
	
	public List<String> getCategoryCodes() {
		return categoryCodes;
	}

	public void setCategoryCodes(List<String> categoryCodes) {
		this.categoryCodes = categoryCodes;
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	/**
	 * 判断是否为当前站点
	 */
	public Boolean getIsCurrentSite(){
		return getCurrentSiteCode().equals(siteCode);
	}
	
	/**
	 * 判断是否为默认（主站）站点
	 */
	public static boolean isMainSite(String siteCode){
		if (siteCode != null){
			String code = siteCode;
			if (Global.isUseCorpModel()){
				String corpCode = CorpUtils.getCurrentCorpCode();
				if (code.startsWith(corpCode + "_")){
					code = code.replaceFirst(corpCode + "_", "");
				}
			}
			return MAIN_SITE_CODE.equals(code);
		}
		return false;
	}
	
	/**
	 * 获取当前编辑的站点编号
	 */
	public static String getCurrentSiteCode(){
		String siteCode = (String)UserUtils.getCache("currentSiteCode");
//		if (StringUtils.isBlank(siteCode)){
//			// 如果没有站点代码，则从Cookie中获取，上次登录后的站点。
//			String cookieName = "siteCode";
//			if (Global.isUseCorpModel()){
//				cookieName = CorpUtils.getCurrentCorpCode() + "_" + cookieName;
//			}
//			siteCode = CookieUtils.getCookie(Servlets.getRequest(), cookieName);
//			UserUtils.putCache("siteCode", siteCode);
//		}
		String defaultSiteCode = MAIN_SITE_CODE;
		if (Global.isUseCorpModel()){
			String corpCode = CorpUtils.getCurrentCorpCode();
			if(!CorpUtils.DEFAULT_CORP_CODE.equals(corpCode)){
				defaultSiteCode = corpCode + "_" + MAIN_SITE_CODE;
			}
		}
		return StringUtils.isNotBlank(siteCode) ? siteCode : defaultSiteCode;
	}

    /**
   	 * 获得模板方案路径。如：/WEB-INF/views/modules/cms/front/themes/jeesite
   	 * @return
   	 */
   	public String getSolutionPath() {
   		return TPL_BASE + "/" + getTheme();
   	}
	
}